All Questions
Tagged with c++programming-practices
73 questions
5votes
5answers
951views
Is it bad practice to use nullptr in ternary operation?
I've set up a ternary operator in place of a pile of if-else's, the final expression being nullptr in order to finish the loop, like so: int menuSelect; std::string operation=""; (...
0votes
5answers
365views
When, if ever, should a private member variable, used in one function, be made into a local static variable?
Lately, I have been playing with this idiom: Changing a private member variable into a local static variable when: member to singleton class used in only one function member is mutex for a shared ...
1vote
0answers
92views
What is the best way to implement the following type of paradigm?
I have the following piece of code. Line 1 is a container (for simplicity, one can think of it as a list of elements e1, e2, ..., en). Now there is a function function_fun which takes as input an ...
6votes
2answers
2kviews
Why would you use 'new' and 'delete' for something that will be referenced through a vector?
I'm going through some code from this article about ECS-systems in game programming and trying to understand it, and something I'm seeing a lot is using heap memory in places where it seems like ...
-1votes
1answer
241views
How to avoid cyclic dependency in UI application
I'm developing an UI application where I ran into an issue with a cyclic dependency. Here is the simplified code, to explain the problem. #include <list> class UiStyle; UiStyle* CreateStyle(); ...
2votes
4answers
873views
C++ - Is it bad practice to use compiler specific functions?
My requirement is simple, I want to be able to count the number of bits in a number. With a little bit of research, I found that MSVC has __popcnt, GCC has __builtin_popcount and so on. At this stage,...
0votes
1answer
564views
Using for_each instead of iterators to avoid iterator invalidation
I am writing a simple custom (special purpose) container and would like to allow for iteration over each element, however, avoid using iterators due to the problem of iterator invalidation. Instead of ...
10votes
6answers
4kviews
Why are people coding "C-style C++"?
In discussions about whether to cast the result of malloc or not when coding C, one common argument is that if you cast the result then you can compile your C code with a C++ compiler. Why would one ...
0votes
2answers
2kviews
Should I use a class with only static members to encapsulate my program?
So I'm writing a network simulator in C++ as part of a university project. Right now, I have a class structure that looks something like: //includes type aliases #include "GlobalTypes.h" //main body ...
3votes
4answers
3kviews
How to use Macros in Programming to make code faster, efficient and compact
Recently I was going through some of the source-codes of the best competitive programmers in the world. I found out that those people use a template while writing programs, preferably in C++. I have ...
2votes
0answers
245views
Managing application versions using Conan and RPM
I have a shared object that I developed. This shared object is packaged in two forms, a Conan package, and an RPM. It is a Qt project, so it has a .pro file associated with it. Up until now, we ...
-5votes
2answers
221views
What is the proper way to unspecify an integer's value in C++? [closed]
// Default initialization int i; // i has an unspecified value return i; // Probably 0, but Unreliable i = 5; // i has a specified value i = int();// This will give it a specified value, 0 i = ...
0votes
1answer
234views
Limitations of using classes as interface
I am an Electrical Engineer now training and working as an embedded software developer, so I have little formal computer science and software design training. I have until a few months ago only worked ...
0votes
1answer
938views
Advantage or disadvantage of using = operator or uniform initializer while using auto keyword
Most examples of auto use the = operator. #include <iostream> int main() { auto a = 1; std::cout << a << '\n'; } Why don't they use the uniform initializer like this? #...
13votes
7answers
12kviews
Best practice to "continue" from inside a nested loop?
Here is a simplified sample. Basically, it does checks on a string from a string list. If the check passes, it will remove that string (filterStringOut(i);), and it is no longer ...